package DistSys;

import java.awt.Color;
import java.text.NumberFormat;

import Element.Geom.AbsElemGeom;
import Element.AbsElem;

public class Zone {

    private static NumberFormat nf;
    static {
        nf = NumberFormat.getNumberInstance();
        nf.setGroupingUsed(false);
    }

    private double zoneNumber;
    private String zoneLabel;
    private Color  zoneColor;

    public Zone() {
        zoneNumber = 0.0;
        zoneLabel = null;
        zoneColor = Color.gray;
    }

    public Zone(double number, Color c){
        zoneNumber = number;
        zoneLabel = null;
        zoneColor = (c != null ? c : Color.gray);
    }

    public void setZoneNumber(double d ){ zoneNumber = d; }
    public double getZoneNumber(){ return zoneNumber; }

    public void setZoneLabel(String l){ zoneLabel = l; }
    public String getZoneLabel(){ return zoneLabel; }

    public String getZoneString() {
        String sZoneString = "Zone " + nf.format(zoneNumber);
        if (zoneLabel != null && zoneLabel.length() > 0)
            sZoneString += " (" + zoneLabel + ")";
        return sZoneString;
    }

    public void setZoneColor(Color c){ zoneColor = c; }
    public Color getZoneColor(){ return zoneColor; }


    /**
     * Retrieves a <code>zone</code> representation of a zone.
     *
     * @param   dZone   the zone as double
     */
    public static Zone getZone(double dZone) {
        for (int i = 0; i < Sys.prjInfo.allZone.size(); i++) {
            Zone z = (Zone)Sys.prjInfo.allZone.elementAt(i);
            if (dZone == z.getZoneNumber()) {
                return z;
            }
        }
        return null;
    }

    /**
     * Retrieves a string representation of a zone.
     *
     * @param   dZone   the zone as double
     */
    public static String getZoneString(double dZone) {
        // We assume that we get a valid zone !!!
        if (dZone < 0) {
            return zoneChoice[ENTIRE_SYSTEM-(int)dZone];
        } else {
            Zone z = getZone(dZone);
            if (z != null)
                return z.getZoneString();
            else return ""+dZone;
        }
    }

    /**
     * Checks whether the given <code>AbsElemGeom</code> fits the given zone.
     *
     * @param   dZone   the zone
     * @param   geom    the element's geometry
     */
    public static boolean getZoneFits(double dZone, AbsElem elem) {
        return (dZone == ENTIRE_SYSTEM ||
                (dZone == STUDY_AREA       && elem.isInStudyArea()) ||
                (dZone == SELECTED_AREA    && elem.getElemGeomObj().isSelected) ||
                (dZone >= 0       && dZone == elem.getData(AbsElem.DATA_ZONE)));
    }

    /**
     * Special zone: Entire System
     */
    public static final int ENTIRE_SYSTEM = -1;
    /**
     * Special zone: Study Area
     */
    public static final int STUDY_AREA    = -2;
    /**
     * Special zone: Selected Area
     */
    public static final int SELECTED_AREA = -3;
    /**
     * String representations of the special zones
     */
    private static final String[] zoneChoice = new String[] {
        "Entire System",
        "Study Area",
        "Selected Area" };
}